package aaaBrowser;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import aaBaolilo.ImageDemo;
import aaBaolilo.LoginAndPub;
import aaBaolilo.OcrX;
import aaaBrowser.model.RegModel;
import aaaBrowser.model.StoModel;

import com.asprise.util.tiff.v;
import com.asprise.util.ui.k;
import com.attilax.Closure;
import com.attilax.core;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.lang.ClipboardUtil;

/**
 * public static String	USERNAME	= "use2015";							//UU u
	public static String	PASSWORD	= "000000";		
 * @author Administrator
 *
 */
public class Reg extends Thread{
    User user=new User();
   
     class User{
        private StringBuffer username;
        private StringBuffer password;
        public StringBuffer getUsername()
        {
            return username;
        }
        public void setUsername(StringBuffer sb)
        {
            this.username = sb;
        }
        public StringBuffer getPassword()
        {
            return password;
        }
        public void setPassword(StringBuffer password)
        {
            this.password = password;
        }
         
    }
	
	public Reg() {
		this.capt=new CaptX();
	}

	public static void main(String[] args) {
		
		//new Reg().start();
	}

	final Display display = new Display();
	atiBrowserExample atiBrowserExampleinstance;

	public User start(RegModel reger,final Map map1) {

		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		shell.setText("Browser example");
		// shell.setVisible(false);
		// shell.setSize(1, 1);
		atiBrowserExampleinstance = new atiBrowserExample(shell);

		shell.open(); // shell.setVisible(false);

		// core.execMeth_Ays(new Runnable() {
		//
		// @Override
		// public void run() {
		// atiBrowserExampleinstance.addProgressListener(pathx
		// .classPath(LoginAndPub.class) + "\\login.js");
		// atiBrowserExampleinstance
		// .gotoUrl("http://demo.destoon.com/v5.0/member/login.php?a0");
		//
		// }
		// }, "--");

		user=reg( reger, map1);

		// atiBrowserExampleinstance.ini();

		// atiBrowserExampleinstance.addProgressListener(pathx
		// .classPath(LoginAndPub.class) + "\\login.js");

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		// icon.dispose();
		atiBrowserExampleinstance.dispose();
		display.dispose();
		return user;
	}

	public  User reg(final RegModel reger,final Map map1) {
	    System.out.println(reger.getCracturl());
	  
		core.execMeth_Ays(new Runnable() {

			@Override
			public void run() {
				System.out.println("=-ajsy");
				display.getDefault().asyncExec(new Runnable() {
					@Override
					public void run() {
                    System.out.println("run()");
						// atiBrowserExampleinstance
						// .gotoUrl("http://www.baidu.com");
					   String url=reger.getCracturl();
					   System.out.println("reg url:"+url);
						atiBrowserExampleinstance
								.gotoUrl(url);
						atiBrowserExampleinstance.addProgressListener(
								pathx.classPath(Reg.class)
										+ "\\reg.js", new Closure() {

									@Override
									public Object execute(Object arg0)
											throws Exception {
									//pub();

										return null;
									}

								});
						atiBrowserExampleinstance.regJsCallbackMeth("getCaptch",new Closure(){

							@Override
							public Object execute(Object arg0) throws Exception {
							    RegModel reg1= seInfo(reger);
							//	setCaptch ();
								
								return null;
							}});

						atiBrowserExampleinstance.gotoUrl();
					}
				});

			}
		}, "");
		return user;
	
}
	CaptX capt;
	
		public void setCaptch() {
			
			 String captchCode =capt.getTxt();
			
			 String js=filex.read(pathx.classPath(Reg.class)+ "\\set_captch.js");
			 js=js.replaceAll("@capt", captchCode);
					 //"1234";
			 System.out.println("exe js:"+ js);
			 atiBrowserExampleinstance.browser.execute(js);
			
		} 
		
		public RegModel  seInfo(RegModel reger){
            
          
		    
	         String base1 = "abcdefghijklmnopqrstuvwxyz_";   
	         String base2="0123456789";
	          Random random = new Random();   
	         StringBuffer sb = new StringBuffer();   
	         StringBuffer sb1 = new StringBuffer();   
	         for (int i = 0; i < 8; i++) {   
	             int number = random.nextInt(base1.length());   
	             sb.append(base1.charAt(number)); 
	             
	         }  
	         for (int i = 0; i < 2; i++) {   
                 int number = random.nextInt(base2.length());   
                 sb.append(base2.charAt(number)); 
                 
             } 
	         for (int i = 0; i < 8; i++) {   
	             int number = random.nextInt(base1.length());   
	             sb1.append(base1.charAt(number)); 
	             
	         }   
	         for (int i = 0; i < 2; i++) {   
                 int number = random.nextInt(base2.length());   
                 sb.append(base2.charAt(number)); 
                 
             } 
	     System.out.println("username"+sb+"password"+sb1);
	         user.setUsername(sb);
	         user.setPassword(sb1);
	         String js=filex.read(pathx.classPath(Reg.class)+ "\\reg.js");
	         reger.setUsername(sb.toString());
	         reger.setPassword(sb1.toString());
	         js=js.replace("@username",sb );
	         js=js.replace("@email",reger.getEmail());
	         js=js.replace("@password",sb1);
	         js=js.replace("@cpassword",sb1);
	         js=js.replace("@truename",reger.getTruename());
	         js=js.replace("@telephone",reger.getTelephone());
	         js=js.replaceAll("@company", reger.getCompany());
	         System.out.println("exe js:"+ js);
	         atiBrowserExampleinstance.browser.execute(js);
	         return reger;
           
       }
		
		
	private void execMeth_AysGui(final Runnable runnable, String string) {
		core.execMeth_Ays(new Runnable() {

			@SuppressWarnings("static-access")
			@Override
			public void run() {
				System.out.println("=-ajsy");
		//		Display default1 = display.getDefault();
			//	runnable.run();
		 	Display.getDefault().asyncExec(runnable);
		//		display.asyncExec(runnable);
			}
		}, string);

	}

	

}
